package com.nus.fellowmark.modules.module.controller;

import java.util.Arrays;
import java.util.Map;

import com.nus.fellowmark.common.response.ServerResponseEntity;
import com.nus.fellowmark.entity.ModuleEntity;
import com.nus.fellowmark.modules.module.service.ModuleService;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.nus.fellowmark.common.utils.PageUtils;



/**
 * 
 *
 * @author Chen Ruoyi
 * @email chenruoyisz@gmail.com
 * @date 2023-02-10  
 */
@RestController
@RequestMapping("module")
@CrossOrigin
public class ModuleController {
    @Autowired
    private ModuleService moduleService;


    @ApiOperation("list modules")
    @RequestMapping("/list")
    public ServerResponseEntity list(@RequestParam Map<String, Object> params){
        PageUtils page = moduleService.queryPage(params);

        return ServerResponseEntity.success("page", page);
    }


    @ApiOperation("select module by id ")
    @RequestMapping("/info/{id}")
    public ServerResponseEntity info(@PathVariable("id") Integer id){
		ModuleEntity module = moduleService.getById(id);

        return ServerResponseEntity.success("module", module);
    }

    @SaCheckRole(value = {"staff", "admin", "super-admin"}, mode = SaMode.OR)
    @ApiOperation("save module")
    @RequestMapping("/save")
    public ServerResponseEntity save(@RequestBody ModuleEntity module){
		moduleService.save(module);
        // get the new module id from the newly created module
        Integer newModuleId = moduleService.getByCode(module.getCode()).getId();
        return ServerResponseEntity.success(newModuleId);
    }

    @ApiOperation("update module")
    @RequestMapping("/update")
    public ServerResponseEntity update(@RequestBody ModuleEntity module){
		moduleService.updateById(module);

        return ServerResponseEntity.success();
    }

    @ApiOperation("delete module")
    @RequestMapping("/delete")
    public ServerResponseEntity delete(@RequestBody Integer[] ids){
		moduleService.removeByIds(Arrays.asList(ids));

        return ServerResponseEntity.success();
    }

}
